package com.york.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.york.auth.application.convert.AuthRoleDTOConverter;
import com.york.auth.application.dto.AuthRoleDTO;
import com.york.auth.domain.entity.AuthRoleBO;
import com.york.auth.domain.service.AuthRoleDomainService;
import com.york.auth.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * AuthRoleController
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class AuthRoleController {

    @Resource
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 校验
     */
    private static void checkRoleInfo(AuthRoleDTO authRoleDTO) {
        Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色唯一标识不能为空");
        Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
    }

    /**
     * 新增角色
     */
    @RequestMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            checkRoleInfo(authRoleDTO);
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch (Exception e) {
            log.error("AuthRoleController.add.error:{}", e.getMessage(), e);
            return Result.fail("新增角色信息失败");
        }
    }

    /**
     * 更新角色信息
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色Id不能为空");
            checkRoleInfo(authRoleDTO);
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch (Exception e) {
            log.error("AuthRoleController.update.error:{}", e.getMessage(), e);
            return Result.fail("更新角色信息失败");
        }
    }

    /**
     * 删除角色信息
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("AuthRoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOToBO(authRoleDTO);
            Boolean result = authRoleDomainService.delete(authRoleBO);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("AuthRoleController.delete.error:{}", e.getMessage(), e);
            return Result.fail("删除角色信息失败");
        }
    }

//    /**
//     * 查询分类
//     */
//    @PostMapping("/queryPrimaryCategory")
//    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
//        try {
//            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE
//                    .convertBoToCategory(subjectCategoryDTO);
//            List<SubjectCategoryBO> subjectCategoryBOList =
//                    subjectCategoryDomainService.queryCategory(subjectCategoryBO);
//            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE
//                    .convertBoToCategoryDTOList(subjectCategoryBOList);
//            return Result.ok(subjectCategoryDTOList);
//        } catch (Exception e) {
//            log.error("SubjectCategoryController.queryPrimaryCategory.error:{}", e.getMessage(), e);
//            return Result.fail("查询失败");
//        }
//    }
//
}
